﻿using System.Collections.Generic;
using System.Linq;
using NFine.Data;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain._02_ViewModel;
using NFine.Domain._03_Entity.InformationCenter;
using NFine.Domain._04_IRepository.InformationCenter;

namespace NFine.Repository.InformationCenter
{
    public class SysMessagesReplyRepository : RepositoryBase<SysMessagesReplyEntity>, ISysMessagesReplyRepository
    {
        public int DeleteReplies(string keyValues)
        {
            try
            {
                //开启事务
                using (var db = new RepositoryBase().BeginTrans())
                {
                    db.Delete<SysMessagesReplyEntity>(t => keyValues.Contains(t.F_Id));
                    return db.Commit();
                }
            }
            catch
            {
                return 0;
            }
        }

        public List<View_Reply> GetReplyList(string messageId)
        {
            var list =
                from r in dbcontext.Set<SysMessagesReplyEntity>().AsQueryable()/*.Where(w => w.F_MessageId == messageId)*/
                join u in dbcontext.Set<UserEntity>().AsQueryable()
                on r.F_CreatorUserId equals u.F_Id
                where r.F_MessageId == messageId
                select new View_Reply
                {
                    F_CreatorUserId = u.F_Id,
                    F_MessageId = r.F_MessageId,
                    F_CreatorTime = r.F_CreatorTime,
                    F_Content = r.F_Content,
                    F_CreatorUserName = u.F_RealName,
                    IsAdmin = (u.F_IsAdministrator != null && !u.F_IsAdministrator.Value)
                };
            return list.OrderBy(o => o.F_CreatorTime).ToList();
        }
    }
}